package com.csj.mvvm

import android.app.Application
import android.content.Context
import com.csj.mvvm.event.Event
import com.csj.mvvm.event.EventMessage
import com.csj.mvvm.utils.*
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.SpinnerStyle
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import kotlin.properties.Delegates


abstract class App : Application() {

    companion object {
        lateinit var instance: App
        var isNightInit by Delegates.notNull<Boolean>()
        fun post(eventMessage: EventMessage) {
            Event.getInstance().post(eventMessage)
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        // activity 生命周期管理
        BaseLifecycleCallback.getInstance().init(this)
        // 屏幕适配
        ScreenAdaptationUtil.setup(this)
        ScreenAdaptationUtil.register(this, 375F, ScreenAdaptationUtil.MATCH_BASE_WIDTH, ScreenAdaptationUtil.MATCH_UNIT_DP)
        // SP
        SPreference.setContext(applicationContext)
        SpUtil.getInstance().setContext(applicationContext)
        isNightInit = NightModeUtil.isNightMode(this)
        NightModeUtil.initNightMode(isNightInit)
    }

    init {
        //设置全局默认配置（优先级最低，会被其他设置覆盖）
        SmartRefreshLayout.setDefaultRefreshInitializer { _, layout ->
            //开始设置全局的基本参数
            layout.setFooterHeight(40f)
            layout.setDisableContentWhenLoading(false)
            layout.setDisableContentWhenRefresh(true) //是否在刷新的时候禁止列表的操作
            layout.setDisableContentWhenLoading(true) //是否在加载的时候禁止列表的操作
            layout.setEnableOverScrollBounce(false)
            layout.setEnableLoadMoreWhenContentNotFull(false)
            layout.setEnableAutoLoadMore(false) //是否启用列表惯性滑动到底部时自动加载更多
            // 全局设置背景颜色和文字颜色 默认透明背景 文字黑色
//            layout.setPrimaryColorsId(R.color.transparent, R.color.color_999999)
//            layout.setPrimaryColors(Color.parseColor("#00000000"), Color.parseColor("#999999"))
        }
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context: Context?, _ ->

            ClassicsHeader(context)// 指定为经典Header，默认是 贝塞尔雷达Header
                .setSpinnerStyle(SpinnerStyle.Translate)
                .setTextSizeTitle(13f)
                .setDrawableArrowSize(15f)
                .setDrawableProgressSize(15f)
                .setDrawableMarginRight(10f)
                .setFinishDuration(0)
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context: Context?, layout: RefreshLayout? ->
            ClassicsFooter(context)
                .setSpinnerStyle(SpinnerStyle.Translate)
                .setTextSizeTitle(13f)
                .setDrawableArrowSize(15f)
                .setDrawableProgressSize(15f)
                .setDrawableMarginRight(10f)
                .setFinishDuration(0)
        }
    }


}